import Vue from 'vue'
import Router from 'vue-router'
import home from '../components/home/home' //
import ElementUI from 'element-ui' //饿了么PC组件库
import 'element-ui/lib/theme-default/index.css' //饿了么PC组件库样式\
Vue.use(Router)
Vue.use(ElementUI)

var routers = [{
		hiddenMenu: true, //隐藏菜单栏
		path: '/login', //登录页    /login
		meta: {
			showName: '登录页'
		},
		//level: 0,
		component: resolve => {
			require(['../components/login/login'], resolve)
		}
	},{
		hiddenMenu: true, //隐藏菜单栏
		path: '/map', //登录页    /login
		meta: {
			showName: '地图页'
		},
		//level: 0,
		component: resolve => {
			require(['../components/map/map'], resolve)
		}
	}, {
		hiddenMenu: true, //隐藏菜单栏
		path: '/welcome', //欢迎页面    /login
		meta: {
			showName: '欢迎页面'
		},
		//level: 0,
		component: resolve => {
			require(['../components/login/welcome'], resolve)
		}
	}, {
		path: '/system',
		ico: 'fa-cog',
		meta: {
			showName: '系统管理',
		},
		//level: 0, //权限
		component: home,
		children: [{
			path: 'staff',
			ico: 'fa-user',
			meta: {
				showName: '人员管理',
			},
			component: resolve => {
				require(['../components/System/SystemUserView/SystemUser'], resolve)
			}
		}, {
			path: 'role',
			meta: {
				showName: '角色管理',
			},
			component: resolve => {
				require(['../components/System/SystemRoleView/SystemRole'], resolve)
			}
		}, {
			path: 'org',
			meta: {
				showName: '部门管理',
			},
			component: resolve => {
				require(['../components/System/SystemOrgView/SystemOrg'], resolve)
			}
		}, {
			path: 'ProjectDictionary',
			meta: {
				showName: '字典管理',
			},
			component: resolve => {
				require(['../components/System/CommonDictionaryView/CommonDictionary.vue'], resolve)
			}
		}, {
			path: 'menuManage',
			meta: {
				showName: '菜单管理',
			},
			component: resolve => {
				require(['../components/System/SystemMenuView/SystemMenu.vue'], resolve)
			}
		}, {
			hiddenMenu: true, //隐藏菜单栏
			path: '/modifyPassword', //修改密码
			meta: {
				showName: '修改密码'
			},
			//level: 0,
			component: resolve => {
				require(['../components/home/modifyPassword.vue'], resolve)
			}
		}]

	},
	{
		path: '/Project',
		ico: 'fa-cog',
		meta: {
			showName: '项目管理',
		},
		component: home,
		children: [{
			path: 'chart',
			ico: 'fa-user',
			meta: {
				showName: '首页',
			},
			component: resolve => {
				require(['../components/home/chart.vue'], resolve)
			}
		},{
			path: 'ProjectMap',
			ico: 'fa-user',
			meta: {
				showName: '项目展示',
			},
			component: resolve => {
				require(['../components/views/Project/ProjectMap.vue'], resolve)
			}
		},{
			path: 'ProjectInfo',
			ico: 'fa-user',
			meta: {
				showName: '项目基本情况',
			},
			component: resolve => {
				require(['../components/views/Project/ProjectInfo.vue'], resolve)
			}
		},{
			path: 'ProjectLand',
			ico: 'fa-user',
			meta: {
				showName: '土地编辑',
			},
			component: resolve => {
				require(['../components/views/Project/ProjectLand.vue'], resolve)
			}
		},{
			path: 'ProjectProgress',
			ico: 'fa-user',
			meta: {
				showName: '项目推进情况',
			},
			component: resolve => {
				require(['../components/views/Project/ProjectProgress.vue'], resolve)
			}
		},{
			path: 'ProjectPlan',
			ico: 'fa-user',
			meta: {
				showName: '项目年度计划',
			},
			component: resolve => {
				require(['../components/views/Project/ProjectPlan.vue'], resolve)
			}
		},{
			path: 'ProjectWork',
			ico: 'fa-user',
			meta: {
				showName: '工作落实',
			},
			component: resolve => {
				require(['../components/views/Project/ProjectWork.vue'], resolve)
			}
		},{
			path: 'ProjectWorkTask',
			ico: 'fa-user',
			meta: {
				showName: '任务管理',
			},
			component: resolve => {
				require(['../components/views/Project/ProjectWorkTask.vue'], resolve)
			}
		},{
			path: 'ProjectWorkReport',
			ico: 'fa-user',
			meta: {
				showName: '工作进展',
			},
			component: resolve => {
				require(['../components/views/Project/ProjectWorkReport.vue'], resolve)
			}
		},{
			path: 'ProjectLandUse',
			ico: 'fa-user',
			meta: {
				showName: '土地利用',
			},
			component: resolve => {
				require(['../components/views/Project/ProjectLandUse.vue'], resolve)
			}
		}]
	},
//	{
//		path: '/Approve',
//		ico: 'fa-cog',
//		meta: {
//			showName: '审核管理',
//		},
//		component: home,
//		children: [{
//			path: 'Approve/ProjectApprove',
//			ico: 'fa-user',
//			meta: {
//				showName: '项目审核',
//			},
//			component: resolve => {
//				require(['../components/views/Approve/ProjectApprove.vue'], resolve)
//			}
//		},{
//			path: 'Approve/ProgressApprove',
//			ico: 'fa-user',
//			meta: {
//				showName: '项目进展情况',
//			},
//			component: resolve => {
//				require(['../components/views/Approve/ProgressApprove.vue'], resolve)
//			}
//		},{
//			path: 'Approve/ProblemApprove',
//			ico: 'fa-user',
//			meta: {
//				showName: '存在问题和困难',
//			},
//			component: resolve => {
//				require(['../components/views/Approve/ProblemApprove.vue'], resolve)
//			}
//		},{
//			path: 'Approve/MeasureApprove',
//			ico: 'fa-user',
//			meta: {
//				showName: '下一步措施',
//			},
//			component: resolve => {
//				require(['../components/views/Approve/MeasureApprove.vue'], resolve)
//			}
//		},{
//			path: 'Approve/TaskApprove',
//			ico: 'fa-user',
//			meta: {
//				showName: '工作任务',
//			},
//			component: resolve => {
//				require(['../components/views/Approve/TaskApprove.vue'], resolve)
//			}
//		},{
//			path: 'Approve/TaskEvolveApprove',
//			ico: 'fa-user',
//			meta: {
//				showName: '工作进展',
//			},
//			component: resolve => {
//				require(['../components/views/Approve/TaskEvolveApprove.vue'], resolve)
//			}
//		},{
//			path: 'Approve/LandUseApprove',
//			ico: 'fa-user',
//			meta: {
//				showName: '土地利用情况',
//			},
//			component: resolve => {
//				require(['../components/views/Approve/LandUseApprove.vue'], resolve)
//			}
//		},{
//			path: 'Approve/YearPlanApprove',
//			ico: 'fa-user',
//			meta: {
//				showName: '年度计划',
//			},
//			component: resolve => {
//				require(['../components/views/Approve/YearPlanApprove.vue'], resolve)
//			}
//		},{
//			path: 'Approve/MonthPlanApprove',
//			ico: 'fa-user',
//			meta: {
//				showName: '月度计划',
//			},
//			component: resolve => {
//				require(['../components/views/Approve/MonthPlanApprove.vue'], resolve)
//			}
//		}]
//	},
	{
		path: '*',
		hiddenMenu: true, //隐藏菜单栏
		redirect: ''
	}
];

const router = new Router({
	routes: routers
})

/*如果存在session缓存且不是管理员就走缓存的页面信息*/
var cacheRouters = JSON.parse(sessionStorage.getItem("routers")),
	isSuper = sessionStorage.getItem("CurrenUser") ? JSON.parse(sessionStorage.getItem("CurrenUser")).isSuper : false
if(cacheRouters && cacheRouters.length > 0 && !isSuper) {
	SetMenuList()
}

function SetAssemMenuList(data, menuList) {
	var list = [];
				//拼装菜单
				for(var item of data) {
					//var menuData = item.attributes.data;

					let menuItem = {
						path: item.url,
						meta: {
							showName: item.name
						},
						//level: (menuData.level - 1),
						ico: item.icon,
						component: home,
					}
					menuItem.children = [];
					if(item.children != undefined) {
						SetMenuChildren(item.children, menuItem);
					} else {
						menuItem.meta.onlyOne = true
					}
					menuList.push(menuItem);
				}
}

function SetMenuChildren(children, menuList) {
	if(children.length > 0) {
					for(var item of children) {
						//var menuData = item;
						let menuItem = {
							path: item.url,
							meta: {
								showName: item.name
							},
							//ico: menuData.icon,
							component: resolve => {
								require([item.pathUrl], resolve)
							},
						}
						if(menuItem.children == undefined) {
							if(item.children != undefined) {
								if(item.children.length > 0) {
									menuItem.children = [];
								}
							}
						}
						menuList.children.push(menuItem);
						if(item.children != undefined) {
							SetMenuChildren(item.children, menuItem);
						}
					}

				}
}

function SetMenuList() {
	menuList = [];
	menuList.push(routers[0]);
	SetAssemMenuList(JSON.parse(sessionStorage.getItem("routers")), menuList);
	router.options.routes = menuList;
	if(menuList[0].child && menuList[0].child.length > 0) {
		if(menuList[0].child[0].child) { //默认走菜单的第一个路径
			/*增加页面重定向*/
			this.AssmmenuList.push({
				path: '*',
				hiddenMenu: true, //隐藏菜单栏
				redirect: menuList[0].child[0].child[0].path
			})
		} else {
			/*增加页面重定向*/
			this.AssmmenuList.push({
				path: '*',
				hiddenMenu: true, //隐藏菜单栏
				redirect: menuList[0].child[0].path
			})
		}
	}
}

var menuList = []
const fn = Vue.prototype
fn.projectType = []
// fn.projectIdList = []
router.beforeEach(async(to, from, next) => {
	/*获得项目类型-end*/
	//判断当前用户是否有权限
	if(sessionStorage.getItem("CurrenUser")) {
		if(to.redirectedFrom == "/*" || to.matched.length == 0) {
			if(sessionStorage.getItem("routers") != null) {
				if(sessionStorage.getItem("routers").indexOf(to.path) == -1) {
					//layer.msg("无效操作"); //在路由地址乱填的情况下，这里做了简单提醒
					next(from);
				} else {
					next();
				}
			} else {
				next();
			}
		} else {
			next();
		}
	} else {
		if(to.path == '/login' && from.path == '/welcome') {//增加欢迎页跳转
			next();
		} else {
			if(to.path != "/welcome") {
				next("/welcome");
			} else {
				next();
			}
		}
	}
})
router.afterEach((to, from, next) => {
	document.title = '成都国际铁路港项目后台管理-' + to.meta.showName
	/*如果按了后退键，左边的菜单也要显示到对应位置*/
	Vue.prototype.eventBus.$emit('all-change')
})
export default router;
export {
	routers
}